package models;

import java.util.*;
import javax.persistence.*;

import play.Logger;
import play.db.ebean.*;
import play.data.format.*;
import play.data.validation.*;

/**
 * User entity managed by Ebean
 */
@Entity
@Table(name="Movie")
public class Movie extends Model {

    @Id
    @Constraints.Required
    @Formats.NonEmpty
    public Long id;

    @Constraints.Required
	@Formats.DateTime(pattern="dd/MM/yyyy")
    public Date release_date;

    public Long hot;
    public Long duration;
	public String poster;
	public String category;
	public String trailer;

    @OneToMany
    public List<Info> infos = new ArrayList<Info>();

    @OneToMany(cascade=CascadeType.ALL)
    public List<MovieSession> sessions = new ArrayList<MovieSession>();

    // -- Queries
    public static Model.Finder<Long,Movie> find = new Model.Finder(Long.class, Movie.class);

    /**
     * Retrieve all movie.
     */
    public static List<Movie> findAll(String language) {
   		//.order().asc("showtime")
        return find.fetch("infos").where().eq("locale",language).gt("sessions.showtime",new Date()).findList();
    }

    public static Movie findById(Long id, String language) {
    	//.orderBy("sessions.showtime asc")
    	return find.fetch("infos").where().eq("id",id).eq("locale",language).gt("sessions.showtime",new Date()).findUnique();
    }

    public static List<Movie> findTrailer(String language) {
    	return find.fetch("infos").where().ne("trailer", "null").eq("locale", language).findList();
    }

    public static List<Movie> findByDate(int year, int month, int day, String language) {
    	Calendar cal = Calendar.getInstance();
		cal.set(Calendar.YEAR, year);
		cal.set(Calendar.MONTH, month-1);
		cal.set(Calendar.DATE, day);
		cal.set(Calendar.HOUR, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.SECOND, 0);
		cal.set(Calendar.AM_PM, Calendar.AM);
		Date start = cal.getTime();
		cal.set(Calendar.HOUR, 11);
		cal.set(Calendar.MINUTE, 59);
		cal.set(Calendar.SECOND, 59);
		cal.set(Calendar.AM_PM, Calendar.PM);
		Date end = cal.getTime();

		List<Movie> movies1 = find.fetch("sessions").where().between("showtime", start, end).findList();
		List<Movie> movies2 = find.fetch("infos").where().eq("locale",language).findList();

    	for (Movie movie1 : movies1) {
			for (Movie movie2 : movies2) {
				if(movie1.id == movie2.id){
					movie1.infos = movie2.infos;
				}
			}
		}

        return movies1;
    }

    public static List<Movie> sale(String language){
    	Calendar cal = Calendar.getInstance();
    	cal.set(Calendar.YEAR, 2013);
		cal.set(Calendar.MONTH, 5-1);
		cal.set(Calendar.DATE, 3);
		Date start = cal.getTime();
		cal.add(Calendar.DATE, 5);
		Date end = cal.getTime();

		List<Movie> movies1 = find.fetch("sessions").where().between("showtime", start, end).findList();
		List<Movie> movies2 = find.fetch("infos").where().eq("locale",language).findList();

    	for (Movie movie1 : movies1) {
			for (Movie movie2 : movies2) {
				if(movie1.id == movie2.id){
					movie1.infos = movie2.infos;
				}
			}
		}

        return movies1;
    }


}

